<template>
  <el-dialog
  v-drag
  :title="!dataForm.dictId ? '新增字典父项' : '修改字典父项'"
  :close-on-click-modal="false" :visible.sync="visible" width="30%"
  @close="dialogClosed"
  >
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="80px">
      <el-form-item label="父项名称" prop="name">
        <el-input size="mini" v-model="dataForm.name" placeholder="父项名称"></el-input>
      </el-form-item>
      <el-form-item label="父项编码" prop="code">
        <el-input size="mini" v-model="dataForm.code" placeholder="父项编码"></el-input>
      </el-form-item>
      <el-form-item label="备注" >
        <el-input type="textarea" size="mini" v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" @click="visible = false">取消</el-button>
      <el-button size="mini" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data() {
      return {
        visible: false,
        dataForm: {
          dictId: null,
          code: '',
          name: '',
          remark: ''
        },
        dataRule: {
          code: [{
            required: true, message: '父项编码不能为空', trigger: 'blur'
          },
          {
            min: 2, max: 30, message: '父项编码在2~15之间', trigger: 'blur'
          }],
          name: [{
            required: true, message: '父项名称不能为空', trigger: 'blur'
          },
          {
            min: 2, max: 30, message: '父项名称在2~15之间', trigger: 'blur'
          }],
        }
      }
    },
    methods: {
      init(dictId) {
        this.dataForm.dictId = dictId
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.dictId) {
            this.$http({
              url: `/base/basedict/info/${this.dataForm.dictId}`,
              method: 'get',
              params: this.$http.adornParams()
            }).then(({
              data
            }) => {
              if (data && data.code === 0) {
                this.dataForm = data.baseDict
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate(async (valid) => {
          if (valid) {
            const res = await this.$http({
              url: `/base/basedict/${!this.dataForm.dictId ? 'save' : 'update'}`,
              method: 'post',
              data: this.$http.adornData(this.dataForm)
            });
            const data = res.data
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(res.msg)
            }
          }
        })
      },
      dialogClosed() {
        this.$refs['dataForm'].resetFields();
      },
    }
  }
</script>
